package com.shawn.integeration.batch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author shawn
 * @description: 自定义批处理配置
 * @date 2024/1/7
 */
@Slf4j
@Configuration
public class MyBatchConfig {
    /**
     * 创建任务对象
     */
    @Resource
    private JobBuilderFactory jobBuilderFactory;

    /**
     * 执行任务对象
     */
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step step() {
        return stepBuilderFactory
                .get("step")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("Hello World！");
                    // 返回的状态
                    return RepeatStatus.FINISHED;
                }).build();
    }

    @Bean
    public Job helloWorld() {
        return jobBuilderFactory
                .get("helloWorld")
                .start(step())
                .build();
    }

}
